package com.oracle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oracle.application.vo.R;
import com.oracle.entity.Cart;
import com.oracle.feign.CartFeign;
import com.oracle.feign.vo.Product;
import com.oracle.service.ICartService;
import com.oracle.utils.RSAUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.*;
import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fjx
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/cart")
@ConfigurationProperties(prefix = "jwt")
@Data
@Slf4j
public class CartController {

        private String publicKey;

        @Autowired
        private ICartService iCartService;

        @Autowired(required = false)
        private CartFeign cartFeign;


        //添加购物车
        @PostMapping("/addCart")
        public R addCart(@RequestParam(value = "bookId")Integer bookId,@RequestParam(value = "buyNum")Integer buyNum,@RequestParam("token")String token){
                System.out.println(publicKey+"------------------------");
                //判断当前用户购物车中是否有该商品，如果有，则合并购物车，即购物车中购买数量加buyNum;如果没有，则添加购物车
                Claims body = Jwts.parser().setSigningKey(RSAUtils.publicKeyToPublic(publicKey)).parseClaimsJws(token).getBody();
                System.out.println(body);
                Integer memberId = (Integer)body.get("memberId");
                System.out.println(memberId);
                Cart cart = iCartService.selectCartByUserIdAndBookId(memberId, bookId);
                //如果该用户购物车中有该商品，则更改购买数量即可
                if(cart!=null){
                        cart.setBuyNum(cart.getBuyNum()+buyNum);
                        //如果库存不足，不能添加到购物车
                        R<Product> productById = cartFeign.getProductById(bookId);
                        Product product=productById.getData();
                        if(product.getPronum()<cart.getBuyNum()){
                                return new R(201,"库存不足",null);
                        }
                        iCartService.saveOrUpdate(cart);
                        return new R(200,"添加购物车成功",null);
                }
                //如果购物车中没有该商品，则添加该商品
                Cart cart1=new Cart();
                cart1.setBookid(bookId);
                cart1.setBuyNum(buyNum);
                R<Product> productById = cartFeign.getProductById(bookId);
                Product product=productById.getData();
                //如果库存不足，不能添加到购物车
                if(product.getPronum()<buyNum){
                        return new R(201,"库存不足",null);
                }
                cart1.setPrice(BigDecimal.valueOf(product.getProprice()));
                cart1.setTotalprice( cart1.getPrice().multiply(new BigDecimal(buyNum)));
                cart1.setUserid(memberId);
                cart1.setStatus(0);
                //保存到购物车数据库
                iCartService.save(cart1);
                return new R(200,"添加购物车成功",null);
        }
        @GetMapping("/delCart")
        public R delCart(Integer bookId){
                Cart cart=new Cart();
                cart.setBookid(bookId);
                QueryWrapper<Cart> queryWrapper=new QueryWrapper<>(cart);
                boolean remove = iCartService.remove(queryWrapper);
                if(remove){
                        return new R(200,"删除购物车成功",null);
                }
                return new R(201,"删除购物车失败",null);
       }
}